package api

import (
	"blog/library"
	"blog/models"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"os"
	"strconv"
	"strings"
	"time"
)

type BlogController struct {
	baseController
}

func (self *BlogController) List() {
	page, _ := self.GetInt("page", 1)
	limit, _ := self.GetInt("limit", 1)
	categoryList, total, _ := models.GetList(models.BlogTable(), page, limit, orm.NewCondition(), "-Id")
	api := library.Api{}
	api.Data = categoryList
	api.Total = total
	self.Data["json"] = api
	self.ServeJSON()
}

func (self *BlogController) Create() {
	blogStruct := models.NewBlog()
	categoryId, err := self.GetInt64("category_id", -1)
	if err != nil {
		self.error(err.Error())
	} else if categoryId == -1 {
		self.error("分类id错误")
	}
	blogStruct.CategoryId = categoryId
	blogStruct.Content = self.GetString("content")
	blogStruct.Title = self.GetString("title")
	blogStruct.Tags = self.GetString("tags")
	if _, err := models.Create(blogStruct); err != nil {
		self.error(err.Error())
	}
	self.success([]int{})
}

func (self *BlogController) Edit() {
	blogStruct := models.NewBlog()
	categoryId, err := self.GetInt64("category_id", -1)
	if err != nil {
		self.error(err.Error())
	} else if categoryId == -1 {
		self.error("分类id错误")
	}
	blogStruct.Id, _ = self.GetInt64("id")
	blogStruct.CategoryId = categoryId
	blogStruct.Content = self.GetString("content")
	blogStruct.Title = self.GetString("title")
	blogStruct.Tags = self.GetString("tags")
	if _, err := models.UpdateByCondition(blogStruct); err != nil {
		self.error(err.Error())
	}
	self.success([]int{})

}

func (self *BlogController) Publish() {
	redis.GetInstance().Select(0).Expire(1000).Set("a", 1)
	self.publishBlog()
	self.success([]int{})
}

func (self *BlogController) publishBlog() {
	list, _, _ := models.GetAllData(models.BlogTable(), orm.NewCondition())
	for _, item := range list {
		id := strconv.FormatInt(item["id"].(int64), 10)
		path := "static/blog-md/blog" + id + ".md"
		f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.ModePerm)
		if err != nil {
			beego.Error(err.Error())
			continue
		}
		category, _ := models.GetById(models.CategoryTable(), item["category_id"].(int))
		newDate := item["create_time"].(time.Time).Format("2006-01-02 15:04:05")
		tags := strings.FieldsFunc(item["tags"].(string), func(r rune) bool {
			if r == ',' {
				return true
			}
			return false
		})
		md := "---\n"
		md += "title: " + item["title"].(string) + "\n"
		md += "date: " + newDate + "\n"
		md += "tags:\n"
		for _, tag := range tags {
			md += "\t- " + strings.ToUpper(tag) + "\n"
		}
		md += "categories:\n"
		md += "\t- " + strings.ToUpper(category["name"].(string)) + "\n"
		md += "---\n"
		md += item["content"].(string)

		_, err = f.WriteString(md)
		if err != nil {
			_ = f.Close()
			continue
		}
		_ = f.Close()
	}
}

func (self *BlogController) Delete() {
	id, err := self.GetInt("id")
	if err != nil {
		self.error("传入错误的id")
	}
	if _, err := models.DeleteById(models.BlogTable(), id); err != nil {
		self.error(err.Error())
	}
	self.success([]int{})
}
